package met.cs664.ai.cox.soc.strategies.placement;

import soc.game.SOCBoard;

import java.util.List;

/**
 * Scores based on the rarity of the resource in comparison to others and available locations to build settlements. Some
 * resources are more valuable in the early game as they facilitate rapid expansion. As an example ore and brick are
 * both only represented on three hexes while the others have four. Brick is essential for building roads, and
 * settlements in the early game.
 *
 * @author mcox
 */
public class ResourceFrequency extends AbstractEvaluator implements PlacementEvaluator {

    public static final float CLAY = 1.0f;
    public static final float ORE = 0.8f;
    public static final float SHEEP = 0.5f;
    public static final float WHEAT = 0.5f;
    public static final float WOOD = 0.5f;

    /**
     * Builds an array that maps SOCBoard resource types to our calculated frequency value
     *
     * resource to index
     * DESERT_HEX = 0;
     * CLAY_HEX = 1;
     * ORE_HEX = 2;
     * SHEEP_HEX = 3;
     * WHEAT_HEX = 4;
     * WOOD_HEX = 5;
     */
    public static final float [] RESOURCE_VALUE = new float [] { 0, CLAY, ORE, SHEEP, WHEAT, WOOD };

    public ResourceFrequency(float weight) {
        super(weight);
    }

    public String toString() {
        return "ResourceFrequency";
    }

    @Override
    public void evaluateCandidates(SOCBoard board, List<PlacementCandidate> candidates) {
        for (PlacementCandidate candidate : candidates) {
            float score = 0;
            for (int hexCoord : getAdjacentHexesToNode(candidate.getCoord())) {
                int hexType = board.getHexTypeFromCoord(hexCoord);
                if (hexType >= 0 && hexType < RESOURCE_VALUE.length) {
                    score += RESOURCE_VALUE[board.getHexTypeFromCoord(hexCoord)];
                }
            }

            candidate.updateWeight(getWeight() * score);
        }
    }
}
